import 'package:flutter/cupertino.dart';
import 'package:flutter_benchu/utils/commonPkg.dart';
import 'package:flutter_benchu/pages/home/index.dart';
import './widgets/drawer.dart';


class TabBarItem extends BottomNavigationBarItem {
  TabBarItem({String title, int iconName, int activityIconName, double size})
      : super(
          title: Text(
            title,
            style: TextStyle(fontSize: 10),
          ),
          icon: Icon(
            IconFontDate(iconName),
            size: size,
          ),
          activeIcon: Icon(
            IconFontDate(activityIconName),
            size: size,
          ),
        );
}

class TabPage extends StatefulWidget {
  @override
  _TabPageState createState() => _TabPageState();
}

class _TabPageState extends State<TabPage> {
  DateTime lastPopTime;

  Locale locale;

  // 底部的图标 和icon
  List<BottomNavigationBarItem> bottomTabs = [];

  // 页面组件
  List<Widget> tabBodies = [
    HomePage(),
    Text('会员中心'),
    Text('购物车'),
    Text('会员中心'),
  ];

  int currentIndex = 0;
  final PageController _controller = new PageController(
    initialPage: 0,
    viewportFraction: 1,
  );

  @override
  void initState() {
    super.initState();
  }

  onWillPop() async {
    // 点击返回键的操作
    if (lastPopTime == null || DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
      lastPopTime = DateTime.now();
      showToast('再按一次退出');
      return;
    } 
    closeApp();
  }
  // 获取bottomTabs
  List getBottomTabs(BuildContext context) {
    var locale = Provider.of<AppNotifier>(context).locale;
    if (bottomTabs.length == 0 || locale!=this.locale) {
      this.locale=locale;
      bottomTabs = [
        TabBarItem(
          title:text(context, Ids.tabHome),
          iconName:IconNameMap.xiaodaohang_zhouye,
          activityIconName:IconNameMap.shouye_tianchong, 
          size:22.sp
        ),
        TabBarItem(
          title:text(context, Ids.tabQuotes),
          iconName:IconNameMap.xiadaohang_hangqing,
          activityIconName:IconNameMap.hangqing_tianchong, 
          size:22.sp
        ),
        TabBarItem(
          title:text(context, Ids.tabTransaction),
          iconName:IconNameMap.xiadaohang_jiaoyi,
          activityIconName:IconNameMap.jiaoyi_tianchong, 
          size:24.sp
        ),
        TabBarItem(
          title:text(context, Ids.tabAsset),
          iconName:IconNameMap.xiadaohang_zichan,
          activityIconName:IconNameMap.qianbao_tianchong, 
          size:22.sp
        ),
      ];
    }
    
    return bottomTabs;
  }

  @override
  Widget build(BuildContext context) {
   
    return WillPopScope(
      onWillPop: (){ onWillPop();},
      child: Scaffold(
        drawer: DrawerWidget(),
        backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
        bottomNavigationBar: BottomNavigationBar(
          selectedFontSize: 10.sp,
          unselectedFontSize: 10.sp,
          type: BottomNavigationBarType.fixed,
          currentIndex: currentIndex,
          items: getBottomTabs(context),
          onTap: (index) {
            setState(() {
              currentIndex = index;
              _controller.jumpToPage(index);
            });
          },
        ),

        // 添加页面缓存
        body: PageView(
          controller: _controller,
          physics: AlwaysScrollableScrollPhysics(),
          children: tabBodies,
          onPageChanged: (index) {
            setState(() {
              currentIndex = index;
            });
          },
        ),
      ),
    );
  }
}